package com.hzqy.web.ac;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hzqy.commons.utils.FileType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.hzqy.commons.utils.ConvertBeanUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.service.product.ProductServiceBean;
import com.hzqy.service.sensitive.SensitiveWordsServiceBean;
import com.hzqy.web.vo.CmContentOrgVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.SysSensitiveWordsVo;

@Controller
@RequestMapping("/sensitive")
public class SensitiveWordsAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
	@Resource
	private SensitiveWordsServiceBean sensitiveWordsServiceBean;
	
	@RequestMapping("/sensitive_list")
	@ResponseBody
	public Object sensitiveList(SysSensitiveWordsVo sysSensitiveWordsVo, int page, int rows) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap =  sensitiveWordsServiceBean.selectSensitiveList(sysSensitiveWordsVo, page, rows);
		return resultMap;
	}
	
	@RequestMapping("/insert_sensitive")
	@ResponseBody
	public Object insertSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.insertSensitive(sysSensitiveWordsVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","新增敏感词成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","新增敏感词失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/delete_sensitive")
	@ResponseBody
	public Object deleteSensitive(int f_ssw_id) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.deleteSensitive(f_ssw_id);
		resultMap.put("resultTitle", "删除结果");
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","删除失败!");
		}
		return resultMap;
	}
	
	@RequestMapping("/select_sensitive")
	@ResponseBody
	public Object selectSensitive(int f_ssw_id) {
		SysSensitiveWordsVo sysSensitiveWordsVo = sensitiveWordsServiceBean.selectSensitive(f_ssw_id);
		return sysSensitiveWordsVo;
	}
	
	@RequestMapping("/update_sensitive")
	@ResponseBody
	public Object updateSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.updateSensitive(sysSensitiveWordsVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","修改内容成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","修改内容失败!flag="+flag);
		}
		return resultMap;
	}
	
	@RequestMapping("/review_sensitive")
	@ResponseBody
	public Object reviewSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.reviewSensitive(sysSensitiveWordsVo);
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","审核成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","审核失败!flag="+flag);
		}
		return resultMap;
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batch_delete_sensitive")
	@ResponseBody
	public Object batchDeleteSensitive(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.batchDeleteSensitive(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量删除成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量删除失败!flag="+flag);
		}
		return resultMap;
	}
	
	/**
	 * 批量上线
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batch_review_sensitive")
	@ResponseBody
	public Object batchReviewSensitive(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.batchReviewSensitive(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量上线成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量上线失败!flag="+flag);
		}
		return resultMap;
	}
	
	/**
	 * 批量下线
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batch_unreview_sensitive")
	@ResponseBody
	public Object batchUnReviewSensitive(String ids) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.batchUnReviewSensitive(ids);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","批量下线成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","批量下线失败!flag="+flag);
		}
		return resultMap;
	}
	
	//批量导入
	/*@RequestMapping("/insert_batch_sensitive")
	@ResponseBody
	public Object insertBatchContent(
			@RequestParam(value = "uploadify") MultipartFile file , 
			HttpServletResponse response) throws Exception{
		response.setContentType("text/html;charset=UTF-8");
		if(FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			String str = sensitiveWordsServiceBean.insertBatchContent(file);
			return str;
		}else {
			return null;
		}
	}*/
	
	
	
	/**
	 * 刷新敏感词过滤库，重新从数据库读取敏感词库，刷新缓存
	 * @param ids
	 * @return
	 */
	@RequestMapping("/refreshSensitiveWord")
	@ResponseBody
	public Object refreshSensitiveWord() {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = sensitiveWordsServiceBean.refreshSensitiveWord();
		if(flag > 0) {
			resultMap.put("success", true);
			resultMap.put("result","刷新敏感词缓存成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","刷新敏感词缓存失败!");
		}
		return resultMap;
	}
	
	@RequestMapping("/checkSensitiveWord")
	@ResponseBody
	public Object checkSensitiveWord(String txt, int type) {
		Map<String,Object> resultMap = sensitiveWordsServiceBean.checkSensitiveWord(txt, type);
		return resultMap;
	}
	
	
}
